package com.atguigu.service.impl;

import com.atguigu.mapper.ScheduleMapper;
import com.atguigu.pojo.Schedule;
import com.atguigu.service.ScheduleService;
import com.atguigu.utils.PageBean;
import com.atguigu.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleServiceImpl implements ScheduleService {

    @Autowired
    private ScheduleMapper scheduleMapper;

    @Override
    public R save(Schedule schedule) {
        int rows=scheduleMapper.insert(schedule);
        if(rows>0){
            return R.ok(null);
        }
        return null;
    }

    @Override
    public R change(Schedule schedule) {
        if(schedule.getId()==null){
            return R.fail("id is null");
        }
        int rows=scheduleMapper.update(schedule);
        if(rows>0){
            return R.ok(null);
        }
        return null;
    }

    @Override
    public R delete(Integer id) {
        int rows=scheduleMapper.deleteById(id);
        if(rows>0){
            return R.ok(null);
        }
        return R.fail(null);
    }

    @Override
    public R page(int pageSize, int currentPage) {
        PageHelper.startPage(currentPage,pageSize);
        List<Schedule> scheduleList=scheduleMapper.querylist();
        PageInfo<Schedule> pageInfo=new PageInfo<>(scheduleList);
        PageBean<Schedule> pageBean=new PageBean<>(currentPage,pageSize,pageInfo.getTotal(),pageInfo.getList());
        R ok = R.ok(pageBean);
        return ok;
    }
}
